package main

import (
	"sync"
	"time"
)

func main() { // 带完蛋通知的生产者消费者
	queue := make(chan int, 5)
	wg := sync.WaitGroup{}

	wg.Add(2)

	go func() {
		defer wg.Done()
		defer close(queue)
		for i := 0; i < 20; i++ {
			queue <- i
			println("product: ", i)
			time.Sleep(time.Millisecond * 100)
		}
	}()

	go func() {
		defer wg.Done()
		for val := range queue {
			println("       consume: ", val)
			time.Sleep(time.Second * 1)
		}
	}()

	wg.Wait()

	println("done")
}

func main1() { // 普通的生产者消费者
	queue := make(chan int, 5)
	wg := sync.WaitGroup{}

	wg.Add(2)

	go func() {
		defer wg.Done()
		defer close(queue)
		for i := 0; i < 20; i++ {
			queue <- i
			println("product: ", i)
			time.Sleep(time.Millisecond * 100)
		}
	}()

	go func() {
		defer wg.Done()
		for val := range queue {
			println("       consume: ", val)
			time.Sleep(time.Second * 1)
		}
	}()

	wg.Wait()

	println("done")
}
